using System;
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Markup;
using Common;
using Common.Enum;
using Controls;
using Report;
using SProject;
using SProject.Paramter;

namespace ProjectView.ProjectControls
{
	public partial class UCRQOption : UCBaseOption, IComponentConnector
	{
		public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(UCRQOption), new PropertyMetadata(false));

		public static readonly DependencyProperty ProjectProperty = DependencyProperty.Register("Project", typeof(Project), typeof(UCRQOption), new UIPropertyMetadata(null));

		public bool IsReadOnly
		{
			get
			{
				return (bool)GetValue(IsReadOnlyProperty);
			}
			set
			{
				SetValue(IsReadOnlyProperty, value);
			}
		}

		public Project Project
		{
			get
			{
				return (Project)GetValue(ProjectProperty);
			}
			set
			{
				SetValue(ProjectProperty, value);
			}
		}

		public UCRQOption()
		{
			InitializeComponent();
			grid.addMenu(new Separator());
			System.Windows.Controls.MenuItem menuItem = new System.Windows.Controls.MenuItem();
			menuItem.Header = Utility.GetMessage("Project_Memu_Data_Export");
			menuItem.Click += MenuExportData_Click;
			grid.IsLineCopy_Paste_Visable = true;
			grid.IsContextMenuVisible = true;
			grid.addMenu(menuItem);
			AddHandler(DataGridEx.OnCopyRowRoutedEvent, new RoutedEventHandler(OnCopyRowRouted_RoutedEvent), true);
			cmbConfidencLevel.ItemsSource = new List<KeyValuePair>
			{
				new KeyValuePair
				{
					Key = 95.0,
					Value = "95.0%"
				},
				new KeyValuePair
				{
					Key = 97.0,
					Value = "97.0%"
				},
				new KeyValuePair
				{
					Key = 99.0,
					Value = "99.0%"
				},
				new KeyValuePair
				{
					Key = 99.5,
					Value = "99.5%"
				},
				new KeyValuePair
				{
					Key = 99.7,
					Value = "99.7%"
				},
				new KeyValuePair
				{
					Key = 99.9,
					Value = "99.9%"
				}
			};
			cmbSD.ItemsSource = new List<KeyValuePair>
			{
				new KeyValuePair
				{
					Key = 1,
					Value = "1"
				},
				new KeyValuePair
				{
					Key = 2,
					Value = "2"
				},
				new KeyValuePair
				{
					Key = 3,
					Value = "3"
				}
			};
			grid.LoadingRow += grid_LoadingRow;
		}

		private void OnCopyRowRouted_RoutedEvent(object sender, RoutedEventArgs e)
		{
			CopyRowRoutedEventArgs copyRowRoutedEventArgs = e as CopyRowRoutedEventArgs;
			if (copyRowRoutedEventArgs != null)
			{
				RQOptionItem rQOptionItem = copyRowRoutedEventArgs.SourceItem as RQOptionItem;
				RQOptionItem rQOptionItem2 = copyRowRoutedEventArgs.TargetItem as RQOptionItem;
				if (rQOptionItem != null && rQOptionItem2 != null)
				{
					rQOptionItem2.RQTargetType = rQOptionItem.RQTargetType;
					rQOptionItem2.Efficiency = rQOptionItem.Efficiency;
					e.Handled = true;
				}
			}
		}

		private void Root_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
		{
			RefreshColumn();
		}

		private void RefreshColumn()
		{
			BasicOption basicOption = base.DataContext as BasicOption;
			if (basicOption != null)
			{
				basicOption.Items.CollectionChanged += Items_CollectionChanged;
				RefreshColumnTubeNameVisibility();
			}
		}

		private void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			RefreshColumnTubeNameVisibility();
		}

		private void RefreshColumnTubeNameVisibility()
		{
			BasicOption basicOption = base.DataContext as BasicOption;
			if (basicOption != null)
			{
				if (basicOption.Items.Select((BasicOptionItem s) => s.TubeNo).Distinct().Count() <= 1 || basicOption.Items.Where((BasicOptionItem s) => !string.IsNullOrEmpty(s.TubeName)).Count() == 0)
				{
					columnTubeName.Visibility = Visibility.Collapsed;
				}
				else
				{
					columnTubeName.Visibility = Visibility.Visible;
				}
			}
		}

		public bool Validate()
		{
			grid.EndEdit();
			bool flag = true;
			RQOption rQOption = base.DataContext as RQOption;
			if (rQOption != null)
			{
				int index = -1;
				int index2 = -1;
				StringBuilder stringBuilder = new StringBuilder();
				if (rQOption.Items.Where((BasicOptionItem s) => ((RQOptionItem)s).RQTargetType == ERQTargetType.Treatment).Count() == 0 || rQOption.Items.Where((BasicOptionItem s) => ((RQOptionItem)s).RQTargetType == ERQTargetType.Control).Count() == 0)
				{
					stringBuilder.AppendFormat(Utility.GetMessage("msg_rq_targetanalysis_error "));
					index2 = 0;
					index = 3 + ((columnTubeName.Visibility == Visibility.Visible) ? 1 : 0);
					flag = false;
				}
				if (!flag && stringBuilder.Length > 0)
				{
					grid.UnselectAllCells();
					System.Windows.MessageBox.Show(stringBuilder.ToString());
					grid.CurrentColumn = grid.Columns[index];
					grid.CurrentItem = rQOption.Items[index2];
					grid.BeginEdit();
				}
			}
			return flag;
		}

		private void MenuExportData_Click(object sender, RoutedEventArgs e)
		{
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.Title = Utility.GetMessage("Save");
			saveFileDialog.Filter = string.Format("{1}{0}|*.{2}|{3}{0}|*.{3}|{4}{0}|*.{4}", Utility.GetMessage("File"), "Excel", "xlsx", "csv", "txt");
			saveFileDialog.FilterIndex = 1;
			if (saveFileDialog.ShowDialog() == DialogResult.OK)
			{
				base.Cursor = System.Windows.Input.Cursors.Wait;
				switch (saveFileDialog.FilterIndex)
				{
				case 1:
					SaveToExcel(saveFileDialog.FileName);
					break;
				case 2:
				case 3:
					SaveToCsv(saveFileDialog.FileName);
					break;
				}
				base.Cursor = System.Windows.Input.Cursors.Arrow;
				System.Windows.MessageBox.Show(Utility.GetMessage("msg_data_export_sussess"));
			}
		}

		public void SaveToExcel(string file)
		{
			ExcelData excelData = new ExcelData();
			ExcelSheetData excelSheetData = new ExcelSheetData();
			excelSheetData.Name = Utility.GetMessage("melting_analysis_parameter");
			excelSheetData.Data = GetData(true);
			excelData.SheetDataList.Add(excelSheetData);
			ExcelOperator.SaveToExcel(file, excelData);
		}

		public void SaveToCsv(string file)
		{
			CsvTxtOperator.SaveToCsv(file, GetData());
		}

		private List<List<string>> GetData(bool includeMergerCellInfo = false)
		{
			List<List<string>> list = new List<List<string>>();
			List<string> list2 = new List<string>();
			foreach (DataGridColumn column in grid.Columns)
			{
				if (column.Visibility == Visibility.Visible)
				{
					list2.Add(column.Header.ToString());
				}
			}
			list.Add(list2);
			List<string> item = new List<string>();
			foreach (RQOptionItem item2 in (IEnumerable)grid.Items)
			{
				list2 = new List<string>();
				list2.Add(item2.TargetName);
				list2.Add(item2.ChannelNo.ToString());
				if (columnTubeName.Visibility == Visibility.Visible)
				{
					list2.Add(item2.TubeName);
				}
				list2.Add((from s in EnumManagement.GetAllRQTargetType()
					where (ERQTargetType)s.Key == item2.RQTargetType
					select s.Value).FirstOrDefault() as string);
				list2.Add(item2.Efficiency.ToString("0"));
				list.Add(list2);
			}
			if (includeMergerCellInfo)
			{
				list.Insert(0, item);
			}
			return list;
		}

		public void UnSelectAll()
		{
			grid.UnselectAllCells();
		}

		private void grid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
		{
			if (IsReadOnly)
			{
				e.Cancel = true;
			}
		}

		private void grid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
		{
		}

		[DebuggerNonUserCode]
		[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
		internal Delegate _CreateDelegate(Type delegateType, string handler)
		{
			return Delegate.CreateDelegate(delegateType, this, handler);
		}
	}
}
